package com.yilin.tms.message.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.*;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.message.MessageData;
import com.yilin.tms.core.entity.message.PushRecord;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.message.service.IPushService;
import com.yilin.tms.core.remote.IUserRemote;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 推送控制器
 *
 * @author zhangjinyuan
 * @date 2022/10/26
 */
@RestController
@RequestMapping("/message/push/")
public class PushController extends BaseController {

    @Resource
    IPushService pushService;

    @Resource
    IUserRemote userRemote;

    /**
     * 获取推送记录分页
     */
    @PostMapping(value = "getNotificationPage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<PushRecord>> getNotificationPage(PushRecord query, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        PageData<PushRecord> pageData = pushService.page(new PageData<>(page, limit), QueryUtil.andQueryWrapper(new QueryWrapper<>(),query));
        return jsonView(true, "获取成功", pageData);
    }

    /**
     * 根据日期删除推送记录
     */
    @PostMapping(value = "pushDeleteByDate")
    @NotNull({"startDate", "endDate"})
    public ReturnData<Object> pushDeleteByDate(@Explain("开始日期") String startDate, @Explain("结束日期") String endDate) {
        pushService.deleteByDate(startDate, endDate);
        return jsonView(true, "删除成功");
    }

    /**
     * 根据id删除推送记录
     */
    @PostMapping(value = "pushDeleteByIds")
    @NotNull("ids")
    public ReturnData<Object> pushDeleteByIds(@Explain("ID数组") String[] ids) {
        pushService.removeByIds(Arrays.asList(ids));
        return jsonView(true, "删除消息成功");
    }

    //----------------------------------------------------------推送功能区------------------------------------------------

    /**
     * 核心推送1
     * 根据用户推送(socket+极光推送)
     */
    @PostMapping(value = "sendSystemNotify")
    @NotNull({"toAccountId","text"})
    @SkipLoginVerify
    @NoLoginSecurity
    public ReturnData<Object> sendSystemNotify(@Explain("接受人ID") String toAccountId, @Explain("消息内容") String text, String originalUrl) {
        Account toAccount = ParamUtil.isSuccess(userRemote.getAccountById(toAccountId));
        ParamUtil.isNull(toAccount, "未查询到接收用户");
        pushService.sendNotificationByUser(toAccount,  MessageData.MessageType.notice, MessageData.DataType.text, text, originalUrl);
        return jsonView(true, "发送成功");
    }

    /**
     * 核心推送2
     * 根据用户推送(socket+极光推送)
     */
    @PostMapping(value = "sendOrderNotify")
    @NotNull({"toAccountId", "text"})
    @SkipLoginVerify
    @NoLoginSecurity
    public ReturnData<Object> sendOrderNotify(@Explain("接受人ID") String toAccountId, @Explain("消息内容") String text, String originalUrl) {
        Account toAccount = ParamUtil.isSuccess(userRemote.getAccountById(toAccountId));
        ParamUtil.isNull(toAccount, "未查询到接收用户");
        pushService.sendNotificationByUser(toAccount,  MessageData.MessageType.order, MessageData.DataType.order, text, originalUrl);
        return jsonView(true, "发送成功");
    }

    /**
     * 更新推送标识(极光推送)
     */
    @PostMapping(value = "updateAliasAndTag")
    @NotNull({"pushId", "alias", "tag"})
    @UserTarget({UserType.shipperUser, UserType.truckerUser})
    public ReturnData<Object> updateAliasAndTag(String pushId, String alias, String tag) {
        pushService.updateAliasAndTag(getLoginUser(), pushId, alias, tag);
        return jsonView(true, "更新成功");
    }
}
